package com.lxb.myforum.service.spec

import java.util.ArrayList

import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root

import org.apache.commons.lang3.StringUtils
import org.springframework.data.jpa.domain.Specification

import com.lxb.myforum.pojo.Reply

class ReplySpec(private val reply: Reply) : Specification<Reply> {

    override fun toPredicate(root: Root<Reply>, query: CriteriaQuery<*>, cb: CriteriaBuilder): Predicate? {
        val predicates = ArrayList<Predicate>()

        if (reply.post != null) {
            predicates.add(cb.equal(root.get<Any>("post"), reply.post))
        }

        if (StringUtils.isNotBlank(reply.content)) {
            predicates.add(cb.like(root.get("content"), "%" + reply.content + "%"))
        }
        query.where(*predicates.toTypedArray())
        return null
    }
}
